package org.bdware.doip.endpoint.doipServer;

import com.google.gson.JsonArray;
import org.apache.log4j.Logger;
import org.bdware.doip.core.doipMessage.DoipMessage;
import org.bdware.doip.core.doipMessage.DoipMessageFactory;
import org.bdware.doip.core.doipMessage.DoipResponseCode;
import org.bdware.doip.core.model.digitalObject.DigitalObject;
import org.bdware.doip.core.model.operations.BasicOperations;
import org.bdware.doip.core.utils.DoipGson;

public abstract class RepositoryHandlerBase implements RepositoryHandler{

    Logger logger = Logger.getLogger(RepositoryHandlerBase.class);
    protected DoipServiceInfo serviceInfo;

    public RepositoryHandlerBase(DoipServiceInfo info){
        serviceInfo = info;
    }

    @Override
    abstract public DoipMessage handleHello(DoipMessage request);

    @Override
    abstract public DoipMessage handleListOps(DoipMessage request);

    @Override
    abstract public DoipMessage handleCreate(DoipMessage request);

    @Override
    abstract public DoipMessage handleUpdate(DoipMessage request);

    @Override
    abstract public DoipMessage handleDelete(DoipMessage request);

    @Override
    abstract public DoipMessage handleRetrieve(DoipMessage request);

    @Op(op = BasicOperations.Unknown)
    public DoipMessage handleUnknown(DoipMessage request) {
        return replyStringWithStatus(request,"Unsupported Operation!", DoipResponseCode.Declined);
    }

    protected DoipMessage replyAllOperations(DoipMessage request){
        JsonArray allBasicOps = new JsonArray();
        for(BasicOperations op : BasicOperations.values()){
            allBasicOps.add(op.getName());
        }
        return replyString(request, DoipGson.getDoipGson().toJson(allBasicOps));
    }

    protected DoipMessage replyDoipServiceInfo(DoipMessage request){
        DigitalObject respDO = serviceInfo.toDigitalObject();
        respDO.needSign();
        return replyDO(request,respDO);
    }

    protected DoipMessage replyDO(DoipMessage request, DigitalObject digitalObject){
        DoipMessage response;
        response = new DoipMessageFactory.DoipMessageBuilder()
                .createResponse(DoipResponseCode.Success,request)
                .setBody(digitalObject)
                .create();
        logger.debug("[response message]" + DoipGson.getDoipGson().toJson(digitalObject.attributes));
        return response;
    }

    protected DoipMessage replyNull(DoipMessage inMsg){
        DoipMessage response;
        response = new DoipMessageFactory.DoipMessageBuilder()
                .createResponse(DoipResponseCode.Success,inMsg)
                .create();
        return response;
    }

    protected DoipMessage replyString(DoipMessage inMsg, String retStr){
        return replyStringWithStatus(inMsg,retStr,DoipResponseCode.Success);
    }

    protected DoipMessage replyBytes(DoipMessage inMsg, byte[] b){
        DoipMessage response;
        response = new DoipMessageFactory.DoipMessageBuilder()
                .createResponse(DoipResponseCode.Success,inMsg)
                .setBody(b)
                .create();
        return response;
    }

    protected DoipMessage replyStringWithStatus(DoipMessage request, String str, DoipResponseCode resp) {
        DoipMessage response;
        response = new DoipMessageFactory.DoipMessageBuilder()
                .createResponse(resp,request)
                .setBody(str.getBytes())
                .create();
        return response;
    }
}
